package com.tentent.fit.pbJavaCode.assembler;

import com.tentent.fit.pbJavaCode.dto.ActInfo;
import com.tentent.fit.pbJavaCode.common.BaseRequestDTO;
import com.tentent.fit.pbJavaCode.common.BaseResponseDTO;
import com.tentent.fit.pbJavaCode.req.QueryActInfoListRequestDTO;
import com.tentent.fit.pbJavaCode.res.QueryActInfoListResponseDTO;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

@Data
public class ApActionManageActTrpcAssembler {

    public static com.tentent.fit.pbJavaCode.dto.ActInfo convertActInfoFromPbToDTO(ActInfo pbProto) {
        if (pbProto == null) {
            return null;
        }
        com.tentent.fit.pbJavaCode.dto.ActInfo dto = new com.tentent.fit.pbJavaCode.dto.ActInfo();
        dto.setActId(pbProto.getActId());
        dto.setActName(pbProto.getActName());
        return dto;
    }

    public static ActInfo convertActInfoFromDTOToPb(com.tentent.fit.pbJavaCode.dto.ActInfo dto) {
        if (dto == null) {
            return null;
        }
        ActInfo.Builder builder = ActInfo.newBuilder();
        if (StringUtils.isNotEmpty(dto.getActId())) {
            builder.setActId(dto.getActId());
        }
        if (StringUtils.isNotEmpty(dto.getActName())) {
            builder.setActName(dto.getActName());
        }
        return builder.build();
    }

    public static QueryActInfoListRequestDTO convertToQueryActInfoListRequestDTO(QueryActInfoListReq pbReq) {
        if (pbReq == null) {
            return null;
        }
        QueryActInfoListRequestDTO dto = new QueryActInfoListRequestDTO();
        // 从header_param转换BaseRequestDTO字段
        HeaderParam headerParam = pbReq.getHeaderParam();
        if (headerParam != null) {
            dto.setAppid(headerParam.getAppid());
            dto.setSign(headerParam.getSign());
            dto.setOpTimestamp(headerParam.getOpTimestamp());
        }
        // 转换PageInfo字段
        com.tentent.fit.pbJavaCode.common.PageInfo pageInfoDTO = convertPageInfoToDTO(pbReq.getPageInfo());
        dto.setPageInfo(pageInfoDTO);
        dto.setActId(pbReq.getActId());
        dto.setActName(pbReq.getActName());
        return dto;
    }

    private static com.tentent.fit.pbJavaCode.common.PageInfo convertPageInfoToDTO(PageInfo pbPageInfo) {
        if (pbPageInfo == null) {
            return null;
        }
        com.tentent.fit.pbJavaCode.common.PageInfo dto = new com.tentent.fit.pbJavaCode.common.PageInfo();
        dto.setPageNum(pbPageInfo.getPageNum());
        dto.setPageSize(pbPageInfo.getPageSize());
        dto.setCurrentPage(pbPageInfo.getCurrentPage());
        dto.setCurrentPageSize(pbPageInfo.getCurrentPageSize());
        dto.setTotalCount(pbPageInfo.getTotalCount());
        dto.setTotalPage(pbPageInfo.getTotalPage());
        return dto;
    }

    public static QueryActInfoListResp convertToQueryActInfoListResp(QueryActInfoListResponseDTO responseDTO) {
        if (responseDTO == null) {
            return null;
        }
        QueryActInfoListResp.Builder builder = QueryActInfoListResp.newBuilder();
        // 从BaseResponseDTO转换header_param字段
        HeaderParam.Builder headerParamBuilder = HeaderParam.newBuilder();
        if (responseDTO.getAppid() != null) {
            headerParamBuilder.setAppid(responseDTO.getAppid());
        }
        if (responseDTO.getSign() != null) {
            headerParamBuilder.setSign(responseDTO.getSign());
        }
        if (responseDTO.getOpTimestamp() != null) {
            headerParamBuilder.setOpTimestamp(responseDTO.getOpTimestamp());
        }
        builder.setHeaderParam(headerParamBuilder.build());
        // 转换PageInfo字段
        PageInfo pbPageInfo = convertPageInfoToPb(responseDTO.getPageInfo());
        builder.setPageInfo(pbPageInfo);
        builder.setActInfoList(responseDTO.getActInfoList());
        return builder.build();
    }

    private static PageInfo convertPageInfoToPb(com.tentent.fit.pbJavaCode.common.PageInfo dto) {
        if (dto == null) {
            return null;
        }
        PageInfo.Builder builder = PageInfo.newBuilder();
        if (dto.getPageNum() != null) {
            builder.setPageNum(dto.getPageNum());
        }
        if (dto.getPageSize() != null) {
            builder.setPageSize(dto.getPageSize());
        }
        if (dto.getCurrentPage() != null) {
            builder.setCurrentPage(dto.getCurrentPage());
        }
        if (dto.getCurrentPageSize() != null) {
            builder.setCurrentPageSize(dto.getCurrentPageSize());
        }
        if (dto.getTotalCount() != null) {
            builder.setTotalCount(dto.getTotalCount());
        }
        if (dto.getTotalPage() != null) {
            builder.setTotalPage(dto.getTotalPage());
        }
        return builder.build();
    }

}
